<?php
class Model_TableEvenement implements Model_ITable
{
	private static $con = null;
	private static $reqSelectAll = null;
	private static $reqSelectOne = null;
	private static $reqInsert = null;
	private static $reqDeleteOne = null;
	private static $reqUpdate = null;
	private static $reqGetAgendaEvenements = null;
        private static $reqCount = null;
	
	public static function setConnexion(MyPdo $pdo)
	{
		self::$con = $pdo;
	}
	
	public static function getConnexion()
	{
		return self::$con;
	}

	public static function init()
	{
		self::$reqSelectAll = self::$con->prepare('SELECT * FROM evenement ORDER BY id_evenement');
		self::$reqSelectOne = self::$con->prepare('SELECT * FROM evenement WHERE id_evenement = :idcond');
		self::$reqDeleteOne = self::$con->prepare('DELETE FROM evenement WHERE id_evenement = :idcond');
		self::$reqInsert = self::$con->prepare('INSERT INTO evenement(date_debut, date_fin, titre, image, contenu, tags, publie, date_creation, date_modification, id_adresse, id_categorie, id_association) VALUES(:datedebut, :datefin, :titre, :image, :contenu, :tags, :publie, :datecreation, :datemodification, :idadresse, :idcategorie, :idassociation)');
		self::$reqUpdate = self::$con->prepare('UPDATE evenement SET
                        date_debut = :datedebut,
                        date_fin = :datefin,
                        titre = :titre,
			image = :image,
			contenu = :contenu,
			tags = :tags,
			publie = :publie,
			date_creation = :datecreation,
			date_modification = :datemodification,
			id_adresse = :idadresse,
			id_categorie = :idcategorie,
                        id_association = :idassociation WHERE id_evenement = :idcond
		');
		self::$reqGetAgendaEvenements = self::$con->prepare('SELECT e.id_evenement AS id_evenement, e.titre AS titre, e.contenu AS contenu, e.tags AS tags, c.nom AS categorie, e.date_debut as date_debut, e.date_fin as date_fin
			FROM
			evenement e
			INNER JOIN categorie c ON c.id_categorie = e.id_categorie
			WHERE e.publie = 1
			ORDER BY e.id_evenement, e.titre, e.contenu, e.tags, c.nom');
                self::$reqCount = self::$con->prepare('SELECT COUNT(*) FROM evenement');
	}

	public static function fetchAll()
	{
		self::$reqSelectAll->execute();
		$results = self::$reqSelectAll->fetchAll();
		return $results;
	}

	public static function fetchOne($id)
	{
		self::$reqSelectOne->execute(array(':idcond' => $id));
		$results = self::$reqSelectOne->fetchAll();
		if( empty($results[0]) )
			return array();
		else
			return $results[0];
	}
	
	public static function delete($id)
	{
		self::$reqDeleteOne->execute(array(':idcond' => $id));
	}
	
	public static function insert($datedebut, $datefin, $titre, $image, $contenu, $tags, $publie, $datecreation, $datemodification, $idadresse, $idcategorie, $idassociation)
	{
		self::$reqInsert->execute(array(
                        ':datedebut' => $datedebut,
                        ':datefin' => $datefin,
			':titre' => $titre,
			':image' => $image,
			':contenu' => $contenu,
			':tags' => $tags,
			':publie' => $publie,
			':datecreation' => $datecreation,
			':datemodification' => $datemodification,
			':idadresse' => $idadresse,
			':idcategorie' => $idcategorie,
                        ':idassociation' => $idassociation
		));
		return self::$con->lastInsertId();
	}
	
	public static function update($id, $datedebut, $datefin, $titre, $image, $contenu, $tags, $publie, $datecreation, $datemodification, $idadresse, $idcategorie, $idassociation)
	{
		self::$reqUpdate->execute(array(
                        ':datedebut' => $datedebut,
                        ':datefin' => $datefin,
			':titre' => $titre,
			':image' => $image,
			':contenu' => $contenu,
			':tags' => $tags,
			':publie' => $publie,
			':datecreation' => $datecreation,
			':datemodification' => $datemodification,
			':idadresse' => $idadresse,
			':idcategorie' => $idcategorie,
                        ':idassociation' => $idassociation,
			':idcond' => $id
		));
	}

        public static function count()
	{
		self::$reqCount->execute();
		return self::$reqCount->fetchColumn();
	}
}
